<?php
/**
 * 影院模型:影院信息
 * Created by PhpStorm.
 * User: zhangshiping
 * Date: 15-10-12
 * Time: 上午9:26
 */

namespace Common\Model;

use Common\Logic\CacheLogic;

class CinemaHostModel extends BaseModel{

    protected $_line = array();
    protected $_link = array(
        'Area' => array( //影院->区域
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Area',
            'foreign_key' => 'area_id',
            'as_fields' => null,
            'mapping_name' => 'Area',
            'condition' => '',
            'relation_tb' => 'area',
            'relation_alias' => 'a',
            'relation_field' => 'area_id',
            'main_table' => 'cinema_host',
            'main_field' => 'area_3',
            'main_alias' => 'ch'
        ),
        'BusinessArea' => array( //影院->商圈
            'mapping_type'  =>self::MANY_TO_MANY,
            'class_name'         => 'BusinessArea',
            'foreign_key'        => 'bsa_id',
            'relation_foreign_key'  => 'cimema_id',
            'relation_table'  => '__CINEMA_BUSINESS_AREA__',
            'mapping_name'       =>'CinemaBusinessArea',
            'mapping_order'      =>'id DESC',
            'mapping_limit'      =>'20',
            'condition'         =>'',
            'relation_tb'       => 'business_area',
            'relation_alias'       => 'ba',
            'relation_field'       =>'bsa_id',
            'main_table'         => 'cinema_host',
            'main_field'         =>'cinema_id',
            'main_alias'         =>'c',
            'middle_table'=>'cinema_business_area',
            'middle_alias'=>'cba'
        ),
        'OrgHall'=>array( //影院->影厅
            'mapping_type'  =>self::HAS_MANY,
            'class_name'         => 'OrgHall',
            'foreign_key'        => 'cinema_id',
            'parent_key'         =>null,
            'mapping_fields'          =>null,
            'mapping_order'      =>'id DESC',
            'mapping_limit'      =>'20',
            'mapping_name'       =>'OrgHall',
            'condition'         =>'',
            'relation_tb'       => 'org_hall',
            'relation_alias'       => 'oh',
            'relation_field'       =>'cinema_id',
            'main_table'         => 'cinema_host',
            'main_field'         =>'id',
            'main_alias'         =>'c'
        ),
        'OrgPlayCount'=>array( //影院->每日记录
            'mapping_type'  =>self::HAS_MANY,
            'class_name'         => 'OrgPlayCount',
            'foreign_key'        => 'cinema_id',
            'parent_key'         =>null,
            'mapping_fields'          =>null,
            'mapping_order'      =>'id DESC',
            'mapping_limit'      =>'20',
            'mapping_name'       =>'OrgPlayCount',
            'condition'         =>'',
            'relation_tb'       => 'org_play_count',
            'relation_alias'       => 'opc',
            'relation_field'       =>'cinema_id',
            'main_table'         => 'cinema_host',
            'main_field'         =>'id',
            'main_alias'         =>'c'
        ),
        'CinemaAttribute'=>array(
            'mapping_type'  =>self::HAS_ONE,
            'class_name'         => 'CinemaAttribute',
            'foreign_key'        => 'cinema_id',
            'as_fields'          =>null,
            'mapping_name'       =>'CinemaAttribute',
            'condition'         =>'',
            'relation_tb'       => 'cinema_attribute',
            'relation_alias'       => 'ca',
            'relation_field'       =>'cinema_id',
            'main_table'         => 'cinema_host',
            'main_field'         =>'id',
            'main_alias'         =>'c'
        )
    );

    /**
     * 根据host_name获取影院ID
     * 参数  $host_name 字符串
     * return 字符串
     */
    public function getIdByName($host_name){
        if(!$host_name)return false;
        $id = CacheLogic::HGET(C('DATA_KEY.CINEMAHOST_ID_NAME_RELATION'),$host_name);
        if(!$id){
            $options['where']['host_name'] = array('eq',$host_name);
            $options['field'] = 'id';
            $data =  D('Common/CinemaHost')->getOne($options);
            if($data===false)return false; //获取数据失败
            $id = $data['id'];
            $id AND CacheLogic::HSET(C('DATA_KEY.CINEMAHOST_ID_NAME_RELATION'),$host_name,$id);
        }
        return $id;
    }
    public function getCinemas($field='*',$where=''){
        return $this->where($where)->filed($field)->select();
    }

}